﻿using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using MXERP.Client.Components.Common;
using MXERP.Infrastructure.Managers;

namespace MXERP.Client.Pages.SignEntry;

public partial class Login
{
    [CascadingParameter]
    public Task<AuthenticationState> AuthState { get; set; } = default!;
    [Inject]
    public ISignEntryManager SignEntryManager { get; set; } = default!;

    private CustomValidation? _customValidation;

    public bool BusySubmitting { get; set; }

    private readonly SignInRequest _signInRequest = new();
    private string TenantId { get; set; } = string.Empty;
    private bool _passwordVisibility;
    private InputType _passwordInput = InputType.Password;
    private string _passwordInputIcon = Icons.Material.Filled.VisibilityOff;

    protected override async Task OnInitializedAsync()
    {
        //if (AuthService.ProviderType == AuthProvider.AzureAd)
        //{
        //    AuthService.NavigateToExternalLogin(NavigationManager.Uri);
        //    return;
        //}
        var authState = await AuthState;
        if (authState.User.Identity?.IsAuthenticated is true)
        {
            NavigationManager.NavigateTo("/");
        }
    }

    private void TogglePasswordVisibility()
    {
        if (_passwordVisibility)
        {
            _passwordVisibility = false;
            _passwordInputIcon = Icons.Material.Filled.VisibilityOff;
            _passwordInput = InputType.Password;
        }
        else
        {
            _passwordVisibility = true;
            _passwordInputIcon = Icons.Material.Filled.Visibility;
            _passwordInput = InputType.Text;
        }
    }

    private void FillAdministratorCredentials()
    {
        //_tokenRequest.Email = MultitenancyConstants.Root.EmailAddress;
        //_tokenRequest.Password = MultitenancyConstants.DefaultPassword;
        //TenantId = MultitenancyConstants.Root.Id;
    }

    private async Task SubmitAsync()
    {
        BusySubmitting = true;
        await SignEntryManager.SignIn(_signInRequest);
        //if (await ApiHelper.ExecuteCallGuardedAsync(
        //    () => AuthService.LoginAsync(TenantId, _tokenRequest),
        //    Snackbar,
        //    _customValidation))
        //{
        //    Snackbar.Add($"Logged in as {_tokenRequest.Email}", Severity.Info);
        //}

        BusySubmitting = false;
    }
}